define([
  'text!' + ELMP.resource('components/filedocList/index.html', 'lib'),
  ELMP.resource('utils/pdm-utils.js', 'project_offline-task')
],
  function (template, pdmUtils) {
    return {
      template,
      name: 'FiledocList',
      props: {
        disabled: { type: Boolean, default: false },
        uploadTab: { type: Boolean, default: true },
        relationTab: { type: Boolean, default: true },
      },
      data() {
        return {
          loading: false,
          activeName: 'first',
          filedata: [],
          relationdata: [],
          dialogVisible: false,
          columns: [
            { title: '序号', type: 'seq', width: 50 },
            { title: '附件名称', field: 'name', minWidth: 100, },
            { title: '操作', slots: { default: 'operate' }, width: 150 }
          ],
          projectId: '',
          projectName: '',
          // 上传附件id
          fileIdList: [],
          // 关联附件id
          relationIdList: []
        }
      },
      methods: {
        savedata() {
          this.fileIdList = this.filedata.map(item => item.objectId)
          this.relationIdList = this.relationdata.map(item => item.objectId)
          this.$emit('save', { fileIdList: this.fileIdList, relationIdList: this.relationIdList })
        },
        save() {
          this.savedata()
          this.dialogVisible = false
        },
        openSelectFile() {
          this.$refs.selectFile.open({ projectId: this.projectId })
        },
        saveFile(data) {
          if (this.relationdata.some(item => item.id === data.id)) {
            $.msg.tips('已关联文件')
            return
          }
          data.name = data.title
          data.objectId = data.id
          data.id = data.fileDataId
          this.relationdata.push(data)
        },
        async deleteRow(row, rowIndex, data, isdelete) {
          await this.$confirm('是否确认删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          if (isdelete) {
            await $.elApi.doc.deleteDoc(row.objectId)
          }
          data.splice(rowIndex, 1)
        },
        open({ projectId, projectName, fileIdList, relationIdList }) {
          if (this.uploadTab) {
            this.activeName = 'first'
          } else {
            this.activeName = 'second'
          }
          this.projectId = projectId
          this.projectName = projectName
          this.fileIdList = fileIdList ?? []
          this.relationIdList = relationIdList ?? []
          this.filedata = []
          this.relationdata = []
          this.dialogVisible = true
          this.getdata()
        },
        async getdata() {
          const [filedata, relationdata] = await Promise.all([
            $.elApi.attachment.projList(this.fileIdList),
            $.elApi.attachment.projList(this.relationIdList)
          ])
          this.filedata = this.handledata(filedata)
          this.relationdata = this.handledata(relationdata)
        },
        handledata(data) {
          const list = Object.values(data.res.data)
            .map(item => item[0])
          return list
        },
        async upload() {
          this.loading = true
          const { file } = await $.VXETable.readFile()
          const formdata = new FormData()
          formdata.append('title', file.name);
          formdata.append('folderId', '');
          formdata.append('type', '');
          formdata.append('description', '');
          formdata.append('contextId', this.projectId);
          formdata.append('contextType', this.projectName);
          formdata.append('stateKey', 'PENDINGAUDIT');
          formdata.append('flag', 'true');
          formdata.append('name', file.name);
          formdata.append('main', file);
          const data = await $.elApi.doc.upload(formdata)
          const formdataDoc = new FormData()
          formdataDoc.append('doc_id', data.id);
          formdataDoc.append('flag', 'true');
          formdataDoc.append('main', file);
          
          try {
            const doc = await $.elApi.doc.docupload(formdataDoc)
            this.filedata = this.filedata.concat(doc)
          } catch (error) {
            
          }
          this.loading = false
          this.savedata()
        },
        // 预览
        preview(row) {
          pdmUtils.viewFile(row.id, row.name, "view")
        },
        download(row) {
          $.el.download(ELMP.url(`/doc/v1/storage/${row.id}/download`))
        },
      }
    }
  })
